package Veterinaria.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import Veterinaria.excepcion.DAOExcepcion;
import Veterinaria.modelo.Dueno;
import Veterinaria.modelo.Paciente;
import Veterinaria.negocio.GestionPaciente;
import Veterinaria.negocio.GestionPersona;

/**
 * Servlet implementation class ModificaPacienteServlets
 */
@WebServlet("/ModificaPacienteServlets")
public class ModificaPacienteServlets extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModificaPacienteServlets() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache, no-store");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");

        JSONArray arrayObj = new JSONArray();
        JSONObject obj1 = new JSONObject();
		GestionPaciente paciente = new GestionPaciente();
		String nom = null;
		
		try {
			String query = request.getParameter("term");
	        query = query.toLowerCase();
	        
	        Collection<Paciente> pacientes = paciente.buscarPorNombre(query);

			for (Paciente pac : pacientes) {
				if (pac.getNombre_paciente().toLowerCase().startsWith(query))
					nom = pac.getNombre_paciente() +" (" + pac.getoDueno().getApellido_persona() + " " + pac.getoDueno().getNombre_persona() + ")";
				obj1.put("label", nom);
				obj1.put("value", pac.getNombre_paciente());
				obj1.put("id", pac.getCodigo_paciente());
				obj1.put("nom",pac.getNombre_paciente());
				obj1.put("raza", pac.getRaza());
				obj1.put("genero", pac.getGenero());
				obj1.put("tipos", pac.getTipo_sangre());
				obj1.put("tamano", pac.getTamano());
				obj1.put("actividad", pac.getActividad());
				obj1.put("peso", pac.getPeso());
				obj1.put("esteril", pac.getEsteril());
				obj1.put("fecnac", pac.getFec_nacimiento());
				obj1.put("notas", pac.getNotas());
				obj1.put("imagen", pac.getImagen_paciente());
				obj1.put("estado", pac.getEstado());
				
				arrayObj.add(obj1.toString());
				obj1.clear();
			}

		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	        out.println(arrayObj);
	        out.close();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
